package com.store.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;

import com.store.application.Company;
import com.store.application.Product;
import com.store.application.Store;
import com.store.view.SpringContext;

/**
 * Servlet implementation class TraitementRechercheSocietes
 */
public class TraitementRechercheSocietes extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Store store;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TraitementRechercheSocietes() {
        super();
        ApplicationContext context = SpringContext.getApplicationContext();
        store =  (Store)context.getBean("store");
    
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		// je vais recuperer les parametres du formulaire
				 if (request.getParameter("allCompanies") != null) {
					 
					 List<Company> companies = store.getAllCompanies();
						request.setAttribute("companies", companies);
						request.getRequestDispatcher("JSP/basic/resultSearchCompanies.jsp").forward(request,response);

				    } else if (request.getParameter("companiesByName") != null) {
				          String nomSociete = (String) request.getAttribute("nomSociete");
				          List<Company> companies =null;
						try {
							companies = store.getCompanieByName(nomSociete);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
							request.setAttribute("companies", companies);
							request.getRequestDispatcher("JSP/basic/resultSearchCompanies.jsp").forward(request,response);
				          
				    	
				    } else if (request.getParameter("companiesBySecteur") != null) {
				          // l'utilisateur veut des societ�s par nom
				    	
				    } else if (request.getParameter("companiesByName") != null) {
				          // l'utilisateur veut des societ�s par nom
				    	
				    } else if (request.getParameter("interval1Contrats") != null && request.getParameter("interval2Contrats") != null) {
				          // l'utilisateur veut afficher les contrats en utilisant un interval
				    	
				    	// je vais ici r�cuper les deux intervalles :
				    	String minInterval =  request.getParameter("interval1Contrats");
				    	String maxInterval =  request.getParameter("interval2Contrats");
				    	
				    	// et c'est bon !
				    	
				    } else {
				    	// redirige vers une page d'erreur ou la page de la recherche.
				    }
		
	}

}
